
var PublicModule = angular.module('PublicModule', []);
PublicModule.controller("PublicCtrl", ['$scope', '$http', '$state', '$cookieStore', '$stateParams', '$location', function ($scope, $http, $state, $cookieStore, $stateParams, $location) {
    $scope.httptit = 'http://www.andthink.cn/gongyu/index.php'//请求头
    $scope.httpSrc = 'http://www.andthink.cn/gongyu/'
    $scope.changeData = function (obj) {
        var str = [];
        for (var p in obj) {
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        }
        return str.join("&");
    }
    // 分页参数
    $scope.pageNum = 1;
    var date = new Date()
    // $cookieStore.put('login',{state:1,id:1})
    // 获取用户accessID
    $scope.login = function () {
        // 判断用户是否登录
        if ($cookieStore.get('ACCESS_TOKEN_time')) {
            if ((Date.parse(date) / 1000 - $cookieStore.get('ACCESS_TOKEN_time')) / 60 / 60 <= 2) {
                console.log($location.search().token)
                console.log($cookieStore.get('login'))
                if (!$location.search().token) {
                    if(!$cookieStore.get('login')){
                        window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc06e1f5f2e887c82&redirect_uri=http%3a%2f%2fwww.andthink.cn%2fgongyu%2findex.php%2findex%2fwx%2findex.html&response_type=code&scope=snsapi_userinfo&state=fuck#wechat_redirect'                        
                    }
                } else {
                    console.log('ssssss')                    
                    $cookieStore.put('token', $stateParams.token)
                    if ($cookieStore.get('login')) {
                        console.log($cookieStore.get('login'))
                    } else {
                        $cookieStore.put('login', { id: $location.search().userid })
                        console.log($cookieStore.get('login'))
                    }
                }

            } else {
                window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc06e1f5f2e887c82&redirect_uri=http%3a%2f%2fwww.andthink.cn%2fgongyu%2findex.php%2findex%2fwx%2findex.html&response_type=code&scope=snsapi_userinfo&state=fuck#wechat_redirect'
                $cookieStore.put('ACCESS_TOKEN_time', Date.parse(date) / 1000)
            }
        } else {
            window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc06e1f5f2e887c82&redirect_uri=http%3a%2f%2fwww.andthink.cn%2fgongyu%2findex.php%2findex%2fwx%2findex.html&response_type=code&scope=snsapi_userinfo&state=fuck#wechat_redirect'
            $cookieStore.put('ACCESS_TOKEN_time', Date.parse(date) / 1000)
        }

    }


    // 收藏与取消收藏
    $scope.showcoll = '取消收藏'
    $scope.showImg = function (val) {
        $scope.showcoll = val;
    }


    //切换
    $scope.changeA = function (val) {
        console.log(val);
        $scope.isActive = val;
        if ($scope.isActive) {
            $('#moren').addClass('okList');
            $('#price').removeClass('okList');
            if ($cookieStore.get('cityName')) {
                $http({
                    method: 'post',
                    url: $scope.httptit + '/index/house/lst.html',
                    data: {
                        style: $stateParams.id,
                        // lng:$cookieStore.get('lng&lat').lng,
                        // lat:$cookieStore.get('lng&lat').lng,
                        lng: 123.1112232,
                        lat: 113.223,
                        count: 5,
                        page: $scope.pageNum,
                        order: 1,
                        id: $cookieStore.get('cityName').id,
                    },
                    dataType: 'json',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    transformRequest: $scope.changeData
                }).then(function successCallback(response) {

                    if (response.data.status == 101) {
                        $scope.PartData = response.data.data;
                        console.log(response.data.data)
                    }else if (response.data.status == 102) {
                        $.alert({
                            title: '提示',
                            text: '未检测到数据，请定位后重试',
                        });
                    }
                }, function errorCallback(response) {
                    console.log(response)
                });
            } else {
                $http({
                    method: 'post',
                    url: $scope.httptit + '/index/house/lst.html',
                    data: {
                        style: $stateParams.id,
                        // lng:$cookieStore.get('lng&lat').lng,
                        // lat:$cookieStore.get('lng&lat').lng,
                        lng: 123.1112232,
                        lat: 113.223,
                        count: 5,
                        page: $scope.pageNum,
                        order: 1,
                    },
                    dataType: 'json',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    transformRequest: $scope.changeData
                }).then(function successCallback(response) {

                    if (response.data.status == 101) {
                        $scope.PartData = response.data.data;
                        console.log(response.data.data)
                    }else if (response.data.status == 102) {
                        $.alert({
                            title: '提示',
                            text: '未检测到数据，请定位后重试',

                        });
                    }
                }, function errorCallback(response) {
                    console.log(response)
                });
            }


            $cookieStore.put("xlOrder", 1)
        } else {
            $('#price').addClass('okList');
            $('#moren').removeClass('okList');
            if ($cookieStore.get('cityName')) {
                $http({
                    method: 'post',
                    url: $scope.httptit + '/index/house/lst.html',
                    data: {
                        style: $stateParams.id,
                        // lng:$cookieStore.get('lng&lat').lng,
                        // lat:$cookieStore.get('lng&lat').lng,
                        lng: 123.1112232,
                        lat: 113.223,
                        count: 5,
                        page: $scope.pageNum,
                        order: 2,
                        id: $cookieStore.get('cityName').id,
                    },
                    dataType: 'json',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    transformRequest: $scope.changeData
                }).then(function successCallback(response) {
                    if (response.data.status == 102) {
                        $.alert({
                            title: '提示',
                            text: '未检测到数据，请定位后重试',
                            onOK: function () {
                                //点击确认
                            }
                        });
                    }
                    if (response.data.status == 101) {
                        $scope.PartData = response.data.data;
                        console.log($scope.PartData)
                    }

                    $cookieStore.put("xlOrder", 2)
                    // alert(JSON.stringify(response.data));
                }, function errorCallback(response) {
                    console.log(response)

                });
            } else {
                $http({
                    method: 'post',
                    url: $scope.httptit + '/index/house/lst.html',
                    data: {
                        style: $stateParams.id,
                        // lng:$cookieStore.get('lng&lat').lng,
                        // lat:$cookieStore.get('lng&lat').lng,
                        lng: 123.1112232,
                        lat: 113.223,
                        count: 5,
                        page: $scope.pageNum,
                        order: 2,
                    },
                    dataType: 'json',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    transformRequest: $scope.changeData
                }).then(function successCallback(response) {
                    if (response.data.status == 102) {
                        $.alert({
                            title: '提示',
                            text: '未检测到数据，请定位后重试',
                            onOK: function () {
                                //点击确认
                            }
                        });
                    }else if (response.data.status == 101) {
                        console.log(response.data.data)
                        $scope.PartData = response.data.data;
                        console.log($scope.PartData)
                    }

                    $cookieStore.put("xlOrder", 2)
                    // alert(JSON.stringify(response.data));
                }, function errorCallback(response) {
                    console.log(response)

                });
            }

        }
    }

    // 判断是否为民宿
    $scope.tiaozhuan = function (type, Hid, tit) {
        if (type == 2) {
            $state.go('partRend', { id: Hid, tit: '民宿' })
        } else {
            $state.go('longDesc', { id: Hid, title: tit })
        }
    }

    $scope.Navigation = function (lat, lng) {
        console.log(lat, lng)
        console.log($cookieStore.get('lng&lat'))
        window.location.href = '//uri.amap.com/navigation?from=' + $cookieStore.get('lng&lat').lng + ',' + $cookieStore.get('lng&lat').lat + ',startpoint&to=' + lng+ ',' + lat  + ',endpoint&mode=car&policy=1&src=mypage&coordinate=gaode&callnative=0'

    }




}]);